using System;
using Cysharp.Threading.Tasks;

namespace Cesium3DTilesSelection
{


    class PlaceholderTileProvider : RasterOverlayTileProvider
    {
        public PlaceholderTileProvider(RasterOverlay owner) : base(owner)
        {

        }

        protected override async UniTask<LoadedRasterOverlayImage> loadTileImage(RasterOverlayTile overlayTile)
        {
            return new LoadedRasterOverlayImage();
        }
    }
    public class RasterOverlayOptions
    {
        /**
         * @brief The maximum number of overlay tiles that may simultaneously be in
         * the process of loading.
         */
        public int maximumSimultaneousTileLoads = 20;

        /**
         * @brief The maximum number of bytes to use to cache sub-tiles in memory.
         *
         * This is used by provider types, such as
         * {@link QuadtreeRasterOverlayTileProvider}, that have an underlying tiling
         * scheme that may not align with the tiling scheme of the geometry tiles on
         * which the raster overlay tiles are draped. Because a single sub-tile may
         * overlap multiple geometry tiles, it is useful to cache loaded sub-tiles
         * in memory in case they're needed again soon. This property controls the
         * maximum size of that cache.
         */
        public Int64 subTileCacheBytes = 16 * 1024 * 1024;

    }
    public class RasterOverlay
    {

        public string name { get; protected set; }
        private RasterOverlayTileProvider _pPlaceholder;
        private RasterOverlayTileProvider _pTileProvider;
        private RasterOverlay pSelf;
        public bool _isLoadingTileProvider;
        public RasterOverlayOptions _options = new RasterOverlayOptions();
        public static implicit operator bool(RasterOverlay self)
        {
            return self == null ? false : true;
        }

        internal RasterOverlayTileProvider getTileProvider()
        {
            return this._pTileProvider ? this._pTileProvider : this._pPlaceholder;
        }

        internal async void loadTileProvider(IPrepareRendererResources pPrepareRendererResources)
        {
            if (this._pPlaceholder)
            {
                return;
            }
            this._pPlaceholder = new PlaceholderTileProvider(this);
            this._isLoadingTileProvider = true;
            var pProvider = await this.createTileProvider(pPrepareRendererResources, this);
            this._pTileProvider = pProvider;
            this._isLoadingTileProvider = false;
        }

        public virtual async UniTask<RasterOverlayTileProvider> createTileProvider(IPrepareRendererResources pPrepareRendererResources, RasterOverlay pOwner)
        {
            // return new RasterOverlayTileProvider(pOwner);
            return null;
        }

        internal bool isBeingDestroyed()
        {
            if (pSelf) return true;
            return false;
        }

        internal void destroySafely(RasterOverlay pOverlay)
        {
            if (pOverlay)
            {
                this.pSelf = pOverlay;
            }
            else if (!this.isBeingDestroyed())
            {
                // Ownership was not transferred and we're not already being destroyed,
                // so nothing more to do.
                return;
            }
        }
    }
}